//
//  SharePopView.m
//  Douyin
//
//  Created by Qiao Shi on 2018/7/30.
//  Copyright © 2018年 Qiao Shi. All rights reserved.
//

#import "SharePopView.h"
#import "Masonry.h"
#import "MHshareRecent.h"
@implementation SharePopView

-(instancetype) initWithCircleInfo
{
    self = [super init];
    if (self) {
        NSArray *topIconsName = [[NSArray alloc] initWithObjects:
                                 @"icon_circle_takevideo",
                                 @"icon_circle_write",
//                                 @"icon_circle_aquestion",
                                 nil];
        NSArray *topTexts = [[NSArray alloc] initWithObjects:
                             @"拍视频",
                             @"发照片",
//                             @"提问题",
                             nil];

        self.frame = SCREEN_FRAME;
        self.backgroundColor = [MHTextColor000 colorWithAlphaComponent:0.4];
        
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGuesture:)]];
        _container = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 170)];
        _container.backgroundColor = [UIColor whiteColor];
        [self addSubview:_container];
        
        
        UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, SCREEN_WIDTH, 170) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10.0f, 10.0f)];
        CAShapeLayer* shape = [[CAShapeLayer alloc] init];
        [shape setPath:rounded.CGPath];
        _container.layer.mask = shape;
        
 
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
        label.textAlignment = NSTextAlignmentCenter;
        label.numberOfLines = 0;
        label.textColor = ColorGray;
        label.font = MediumFont;
        [_container addSubview:label];
        CGFloat itemWidth = (SCREEN_WIDTH- 80)/3  ;
        
        UIScrollView *topScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, 90)];
        topScrollView.contentSize = CGSizeMake(itemWidth * topIconsName.count, 80);
        topScrollView.showsHorizontalScrollIndicator = NO;
        topScrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 30);
        [_container addSubview:topScrollView];
        
        for (NSInteger i = 0; i < topIconsName.count; i++) {
            ShareItem *item = [[ShareItem alloc] initWithFrame:CGRectMake( 20*(i+1) + itemWidth*i, 0, itemWidth, 90)];
            item.icon.image = [UIImage imageNamed:topIconsName[i]];
            item.label.text = topTexts[i];
            item.tag = i;
            [item addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onComposeItemTap:)]];
            [item startAnimation:i*0.03f];
            [topScrollView addSubview:item];
        }
        _cancel = [[UIButton alloc] initWithFrame:CGRectMake(0, 110, SCREEN_WIDTH, 50)];
        [_cancel setImage:[UIImage imageNamed:@"icon_circle_close"] forState:0];
        _cancel.font = BigFont;
        _cancel.backgroundColor = MHWhiteTextColor;
        [_container addSubview:_cancel];
        
        UIBezierPath* rounded2 = [UIBezierPath bezierPathWithRoundedRect:_cancel.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10.0f, 10.0f)];
        CAShapeLayer* shape2 = [[CAShapeLayer alloc] init];
        [shape2 setPath:rounded2.CGPath];
        _cancel.layer.mask = shape2;
        [_cancel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGuesture:)]];
    }
    return self;
}


-(instancetype) initWithNameArr:(NSArray *)nameArr iconsNames:(NSArray *) iconNames containerHeight:(CGFloat ) containerHeight itemHeight:(CGFloat) itemHeight showHeader:(BOOL) showHeader headerText:(NSString *)headerText
{
    self = [super init];
    if (self) {
        
        self.frame = SCREEN_FRAME;
        self.backgroundColor = [MHTextColor000 colorWithAlphaComponent:0.4];
        
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGuesture:)]];
        _container = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, containerHeight)];
        _container.backgroundColor = showHeader ? MH_MAIN_BACKGROUNDCOLOR_2 : [UIColor whiteColor];
        [self addSubview:_container];
        //设置圆角
        UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, SCREEN_WIDTH, containerHeight) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10.0f, 10.0f)];
        CAShapeLayer* shape = [[CAShapeLayer alloc] init];
        [shape setPath:rounded.CGPath];
        _container.layer.mask = shape;
        
        UILabel *label;
        if (showHeader) {
            label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
            label.textAlignment = NSTextAlignmentCenter;
            label.numberOfLines = 0;
            label.textColor = MHColorFromHexString(@"#D85042");
            label.font = MediumFont;
            label.text = headerText;
            [_container addSubview:label];
        }

        CGFloat itemWidth = (SCREEN_WIDTH- 80)/iconNames.count;
        UIScrollView *topScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, showHeader ? label.bottom : 10.0 , SCREEN_WIDTH, itemHeight)];
        topScrollView.contentSize = CGSizeMake(itemWidth * iconNames.count, itemHeight - 10 );
        topScrollView.showsHorizontalScrollIndicator = NO;
        topScrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 30);
        [_container addSubview:topScrollView];
    
        for (NSInteger i = 0; i < iconNames.count; i++) {
            ShareItem *item = [[ShareItem alloc] initWithFrame:CGRectMake( 20*(i+1) + itemWidth*i, 0, itemWidth, itemHeight)];
            item.icon.image = [UIImage imageNamed:iconNames[i]];
            item.label.text = nameArr[i];
            item.tag = i;
            [item addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onComposeItemTap:)]];
            [item startAnimation:i*0.03f]; //开始动画
            if (iconNames.count == 1) {
                item.centerX = self.centerX;
                item.tag = 1;
            }
            [topScrollView addSubview:item];
        }
        
        _cancel = [[UIButton alloc] initWithFrame:CGRectMake(0, showHeader ? containerHeight - 50 : CGRectGetMaxY(topScrollView.frame), SCREEN_WIDTH, 50)];
        [_cancel setImage:[UIImage imageNamed:@"icon_circle_close"] forState:0];
        _cancel.titleLabel.font = BigFont;
        _cancel.backgroundColor = MHWhiteTextColor;
        [_container addSubview:_cancel];
        
        UIBezierPath* rounded2 = [UIBezierPath bezierPathWithRoundedRect:_cancel.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10.0f, 10.0f)];
        CAShapeLayer* shape2 = [[CAShapeLayer alloc] init];
        [shape2 setPath:rounded2.CGPath];
        _cancel.layer.mask = shape2;
        [_cancel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGuesture:)]];
    }
    return self;
    
}


- (void)onComposeItemTap:(UITapGestureRecognizer *)sender
{
    switch (sender.view.tag) {
            
        case 0 :case 1: case 2:
        {
            if (self.circleBottonCilckBlock) {
                self.circleBottonCilckBlock(sender.view.tag);
            }
        }
            break;
        default:
            break;
    }
}

-(instancetype) initWithAwmeModel:(MHAwemeModel*)awemeModel{
    self = [super init];
    if (self) {
        
        NSArray *topIconsName;
        NSArray *topTexts;
        _awemeModel = awemeModel;
        if (_awemeModel.shareTimeline) {
            topIconsName = [[NSArray alloc] initWithObjects:
                             @"ic_share_pyq",
                             @"circle_collectionnor",
                             nil];
            topTexts = [[NSArray alloc] initWithObjects:
                         @"朋友圈",
                         @"收藏",
                         nil];
        }else
        {
            topIconsName = [[NSArray alloc] initWithObjects:
                             @"ic_share_wx",
                             @"ic_share_pyq",
                             @"circle_collectionnor",
                             nil];
            topTexts = [[NSArray alloc] initWithObjects:
                         @"微信",
                         @"朋友圈",
                         @"收藏",
                         nil];
        }
        
        self.frame = SCREEN_FRAME;
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGuesture:)]];
        _container = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 170)];
        _container.backgroundColor = MH_MAIN_BACKGROUNDCOLOR_2;
        [self addSubview:_container];
        
        
        UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, SCREEN_WIDTH, 170) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10.0f, 10.0f)];
        CAShapeLayer* shape = [[CAShapeLayer alloc] init];
        [shape setPath:rounded.CGPath];
        _container.layer.mask = shape;
        
        UIBlurEffect *blurEffect =[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
        visualEffectView.frame = self.bounds;
        visualEffectView.alpha = 1.0f;
        [_container addSubview:visualEffectView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
        label.textAlignment = NSTextAlignmentCenter;
        label.numberOfLines = 0;
        label.textColor = ColorGray;
        label.font = MediumFont;
        [_container addSubview:label];
        
        
        CGFloat itemWidth = 68;
        
        UIScrollView *topScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, 90)];
        topScrollView.contentSize = CGSizeMake(itemWidth * topIconsName.count, 80);
        topScrollView.showsHorizontalScrollIndicator = NO;
        topScrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 30);
        [_container addSubview:topScrollView];
        
        for (NSInteger i = 0; i < topIconsName.count; i++) {
            ShareItem *item = [[ShareItem alloc] initWithFrame:CGRectMake(20 + itemWidth*i, 0, 48, 90)];
            if(_awemeModel.isHaveCollect &&  [topIconsName[i] isEqualToString:@"circle_collectionnor"])
            {
                item.icon.image = [UIImage imageNamed:@"circle_collectionsel"];
            }else
            {
                item.icon.image = [UIImage imageNamed:topIconsName[i]];
            }
            
            item.label.text = topTexts[i];
            item.tag =  _awemeModel.shareTimeline ? i+ 1 :  i;
            [item addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onShareItemTap:)]];
            [item startAnimation:i*0.03f];
            [topScrollView addSubview:item];
        }
        _cancel = [[UIButton alloc] initWithFrame:CGRectMake(0, 120, SCREEN_WIDTH, 50)];
        _cancel.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_cancel setTitle:@"取消" forState:UIControlStateNormal];
        [_cancel setTitleColor:MHTextColor333 forState:UIControlStateNormal];
        _cancel.titleLabel.font = BigFont;
        _cancel.backgroundColor = MHWhiteTextColor;
        [_container addSubview:_cancel];
        
        UIBezierPath* rounded2 = [UIBezierPath bezierPathWithRoundedRect:_cancel.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10.0f, 10.0f)];
        CAShapeLayer* shape2 = [[CAShapeLayer alloc] init];
        [shape2 setPath:rounded2.CGPath];
        _cancel.layer.mask = shape2;
        [_cancel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGuesture:)]];
    }
    return self;
}

- (void)onShareItemTap:(UITapGestureRecognizer *)sender {
    switch (sender.view.tag) {
            
        case 0:
        {
            
            [[[[MHHTTPService sharedInstance] shareRecentStoryWithCircleId:_awemeModel.circleId recentId:_awemeModel.recentId] mh_parsedResults] subscribeNext:^(MHshareRecent *share) {
                
                [MHMomentHelper shareWithSharRecent:share shareType:MHCirclShareTypeWeChat];
            }];
            
        }
            break;
        case 1:
        {
            [[[[MHHTTPService sharedInstance] shareRecentStoryWithCircleId:_awemeModel.circleId recentId:_awemeModel.recentId] mh_parsedResults] subscribeNext:^(MHshareRecent *share) {
                [MHMomentHelper shareWithSharRecent:share shareType:MHCirclShareTypeWeChatTimeLine];
            }];
        }
            
            break;
        case 2:
        {
            if(_awemeModel.isHaveCollect)
            {
                @weakify(self);
                [[[MHHTTPService sharedInstance] cancelCollectMarkWithRecentId:_awemeModel.recentId] subscribeNext:^(id x) {
                    @strongify(self);
                    [UIWindow showTips:@"取消收藏成功"];
                    self.awemeModel.isHaveCollect = false;
                }];
            }else
            {
                @weakify(self);
                [[[MHHTTPService sharedInstance] addRecentCollectMarkWithRecentId:_awemeModel.recentId]subscribeNext:^(id x) {
                    @strongify(self);
                    [UIWindow showTips:@"收藏成功"];
                    self.awemeModel.isHaveCollect = YES;
                }];
            }
        }
            
            break;
        default:
            break;
    }
    [self dismiss];
}
- (void)onActionItemTap:(UITapGestureRecognizer *)sender {
    switch (sender.view.tag) {
        case 0:
            break;
        default:
            break;
    }
    [self dismiss];
}

- (void)handleGuesture:(UITapGestureRecognizer *)sender {
    CGPoint point = [sender locationInView:_container];
    if(![_container.layer containsPoint:point]) {
        [self dismiss];
        return;
    }
    point = [sender locationInView:_cancel];
    if([_cancel.layer containsPoint:point]) {
        [self dismiss];
    }
}

- (void)show {
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    [window addSubview:self];
    [UIView animateWithDuration:0.15f
                          delay:0.0f
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         CGRect frame = self.container.frame;
                         frame.origin.y = frame.origin.y - frame.size.height;
                         self.container.frame = frame;
                     }
                     completion:^(BOOL finished) {
                     }];
}

- (void)dismiss {
    [UIView animateWithDuration:0.15f
                          delay:0.0f
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         CGRect frame = self.container.frame;
                         frame.origin.y = frame.origin.y + frame.size.height;
                         self.container.frame = frame;
                     }
                     completion:^(BOOL finished) {
                         [self removeFromSuperview];
                     }];
}

@end



#pragma Item view

@implementation ShareItem
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _icon = [[UIImageView alloc] init];
        _icon.image = [UIImage imageNamed:@"iconHomeAllshareCopylink"];
        _icon.contentMode = UIViewContentModeScaleToFill;
        _icon.userInteractionEnabled = YES;
        [self addSubview:_icon];
        
        _label = [[UILabel alloc] init];
        _label.text = @"TEXT";
        _label.textColor = MHAlertTextColor;
        _label.font = MediumFont;
        _label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_label];
    }
    return self;
}
-(void)startAnimation:(NSTimeInterval)delayTime {
    CGRect originalFrame = self.frame;
    self.frame = CGRectMake(CGRectGetMinX(originalFrame), 35, originalFrame.size.width, originalFrame.size.height);
    [UIView animateWithDuration:0.9f
                          delay:delayTime
         usingSpringWithDamping:0.5f
          initialSpringVelocity:0.0f
                        options:UIViewAnimationOptionCurveEaseInOut  | UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         self.frame = originalFrame;
                     }
                     completion:^(BOOL finished) {
                     }];
}

-(void)layoutSubviews {
    [super layoutSubviews];
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.height.mas_equalTo(48);
        make.centerX.equalTo(self);
        make.top.equalTo(self).offset(10);
    }];
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.icon.mas_bottom).offset(10);
    }];
}
@end
